################################################################################################
# This script takes the clean QV/SV data from the splitting script and filters sujects who
# fall under any one of these categories:
# 
# - SV: Cast a BV on abstain. 
# - BV: Cast a Red Vote on Abstain. Can also be modified to remove any vote on abstain. See QV. 
# 
# Rewrites the clean dataframes for QV and SV. 
# 
# Author: Luis S.
# Last Modified: 7.23.16
################################################################################################

library (dplyr)

# setwd("C:/Users/Luis/Dropbox/Research/Casella/Storable Votes (MTurk) - California Experiment/California Experiment Results/")

####################################
####### SV Subject Filtering #######
####################################

SV_data <- read.csv("California Stage 2 - SV & QV Data/Clean Data/California_MTurk_SV_Stage_2_clean.csv", header = T, stringsAsFactors = F)

######################### Remove those who cast BV on Abstain ####################

SV_data <- mutate(SV_data, RemoveSubject = "No")

for(row in 1:nrow(SV_data))
{
  ### Bilingual Education ### 
  if((SV_data$Ref1_BilingualEduc[row] == "Abstain") & (SV_data$SV1_Vote[row] == "BilingualEduc")) {SV_data$RemoveSubject[row] <- NA}
  if((SV_data$Ref1_BilingualEduc[row] == "Abstain") & (SV_data$SV2_Vote[row] == "BilingualEduc")) {SV_data$RemoveSubject[row] <- NA}
  
  ### Teacher Tenure ###
  if((SV_data$Ref2_TeacherTenure[row] == "Abstain") & (SV_data$SV1_Vote[row] == "TeacherTenure")) {SV_data$RemoveSubject[row] <- NA}
  if((SV_data$Ref2_TeacherTenure[row] == "Abstain") & (SV_data$SV2_Vote[row] == "TeacherTenure")) {SV_data$RemoveSubject[row] <- NA}
  
  ### Public Bonds ###
  if((SV_data$Ref3_PublicBonds[row] == "Abstain") & (SV_data$SV1_Vote[row] == "PublicBonds")) {SV_data$RemoveSubject[row] <- NA}
  if((SV_data$Ref3_PublicBonds[row] == "Abstain") & (SV_data$SV2_Vote[row] == "PublicBonds")) {SV_data$RemoveSubject[row] <- NA}
  
  ### Immigration ###
  if((SV_data$Ref4_Immigration[row] == "Abstain") & (SV_data$SV1_Vote[row] == "Immigration")) {SV_data$RemoveSubject[row] <- NA}
  if((SV_data$Ref4_Immigration[row] == "Abstain") & (SV_data$SV2_Vote[row] == "Immigration")) {SV_data$RemoveSubject[row] <- NA}
}

SV_data <- na.omit(SV_data)
SV_data <- select(SV_data, -matches("RemoveSubject"))
write.csv(SV_data, "California Stage 2 - SV & QV Data/Clean Data/California_MTurk_SV_Stage_2_clean.csv", row.names = F)

####################################
####### QV Subject Filtering #######
####################################

QV_data <- read.csv("California Stage 2 - SV & QV Data/Clean Data/California_MTurk_QV_Stage_2_clean.csv", header = T, stringsAsFactors = F)

######################### Remove those who cast Red Votes on Abstain (and maybe other votes) ####################

QV_data <- mutate(QV_data, RemoveSubject = "No")

# if you want to remove the vote set to TRUE, otherwise set to FALSE 
# Only Uncomment if not done through master script...
# removeRedVote <- TRUE
# removeOtherVotes <- FALSE

for(row in 1:nrow(QV_data))
{
  ### Bilingual Education ###
  if((QV_data$Ref1_BilingualEduc[row] == "Abstain") & (!is.na(QV_data$QV1_Vote_BilingualEduc[row])))
  {
    if(QV_data$QV1_Class[row] == "(1) Red" & removeRedVote) {QV_data$RemoveSubject[row] <- "Yes"}
    if(QV_data$QV1_Class[row] != "(1) Red" & removeOtherVotes) {QV_data$RemoveSubject[row] <- "Yes"}  
  }

  if((QV_data$Ref1_BilingualEduc[row] == "Abstain") & (!is.na(QV_data$QV2_Vote_BilingualEduc[row])))
  {
    if(QV_data$QV2_Class[row] == "(1) Red" & removeRedVote) {QV_data$RemoveSubject[row] <- "Yes"}
    if(QV_data$QV2_Class[row] != "(1) Red" & removeOtherVotes) {QV_data$RemoveSubject[row] <- "Yes"}  
  }

  ### Teacher Tenure ###
  if((QV_data$Ref2_TeacherTenure[row] == "Abstain") & (!is.na(QV_data$QV1_Vote_TeacherTenure[row])))
  {
    if(QV_data$QV1_Class[row] == "(1) Red" & removeRedVote) {QV_data$RemoveSubject[row] <- "Yes"}
    if(QV_data$QV1_Class[row] != "(1) Red" & removeOtherVotes) {QV_data$RemoveSubject[row] <- "Yes"}  
  }

  if((QV_data$Ref2_TeacherTenure[row] == "Abstain") & (!is.na(QV_data$QV2_Vote_TeacherTenure[row])))
  {
    if(QV_data$QV2_Class[row] == "(1) Red" & removeRedVote) {QV_data$RemoveSubject[row] <- "Yes"}
    if(QV_data$QV2_Class[row] != "(1) Red" & removeOtherVotes) {QV_data$RemoveSubject[row] <- "Yes"}  
  }

  ### Public Bonds ###
  if((QV_data$Ref3_PublicBonds[row] == "Abstain") & (!is.na(QV_data$QV1_Vote_PublicBonds[row])))
  {
    if(QV_data$QV1_Class[row] == "(1) Red" & removeRedVote) {QV_data$RemoveSubject[row] <- "Yes"}
    if(QV_data$QV1_Class[row] != "(1) Red" & removeOtherVotes) {QV_data$RemoveSubject[row] <- "Yes"}  
  }

  if((QV_data$Ref3_PublicBonds[row] == "Abstain") & (!is.na(QV_data$QV2_Vote_PublicBonds[row])))
  {
    if(QV_data$QV2_Class[row] == "(1) Red" & removeRedVote) {QV_data$RemoveSubject[row] <- "Yes"}
    if(QV_data$QV2_Class[row] != "(1) Red" & removeOtherVotes) {QV_data$RemoveSubject[row] <- "Yes"}  
  }
  
  ### Immigration ###
  if((QV_data$Ref4_Immigration[row] == "Abstain") & (!is.na(QV_data$QV1_Vote_Immigration[row]))) 
  {
    if(QV_data$QV1_Class[row] == "(1) Red" & removeRedVote) {QV_data$RemoveSubject[row] <- "Yes"}
    if(QV_data$QV1_Class[row] != "(1) Red" & removeOtherVotes) {QV_data$RemoveSubject[row] <- "Yes"}
  }
  
  if((QV_data$Ref4_Immigration[row] == "Abstain") & (!is.na(QV_data$QV2_Vote_Immigration[row]))) 
  {
    if(QV_data$QV2_Class[row] == "(1) Red" & removeRedVote) {QV_data$RemoveSubject[row] <- "Yes"}
    if(QV_data$QV2_Class[row] != "(1) Red" & removeOtherVotes) {QV_data$RemoveSubject[row] <- "Yes"}  
  }
}

QV_data <- filter(QV_data, RemoveSubject == "No") %>% select(-matches("RemoveSubject"))
write.csv(QV_data, "California Stage 2 - SV & QV Data/Clean Data/California_MTurk_QV_Stage_2_clean.csv", row.names = F)
